﻿using System;

namespace jSLCore.Client.Presentation.jBinding
{
    /// <summary>
    /// Binding for custom data providing by delegate
    /// </summary>
    public class jDelegateValueBinding : jReadOnlyBinding
    {
        public override object GetValueForTarget()
        {
            return Convert(InvokeProvideValue());
        }

        public event EventHandler<GenericEventArgs<object>> ProvideValue;

        public object InvokeProvideValue()
        {
            EventHandler<GenericEventArgs<object>> handler = ProvideValue;
            if (handler != null)
            {
                var result = new GenericEventArgs<object>();
                handler(this, result);
                return result.Parameter;
            }
            
            //We dont want call exception, because of design time
           //throw new NotImplementedException("You have to provide value for jDelegateValueBinding by submiting ProvideValue event");
            return null;
        }
    }
}